/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import queenb.net.entitybean.F000;
import queenb.net.entitybean.M650;
import queenb.web.store.F000Store;
import queenb.web.store.M650Store;
import queenb.web.util.ComboboxUtil;
import queenb.web.util.ResourcesProvider;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;

/**
 *
 * @author ems
 */
public class M650Controller extends QueenbController {
    
    private int pn100;
    private int pm650;
    private String avata;
    private String flag = "none";
    private List<M650> listdocument = new ArrayList<M650>();
    private String table;
    private boolean add = false;
    private boolean edit = false;
    private boolean delete = false;
    private String active = "";
    private int fv050;
    private int fs200;
    private int pf000;// id thư mục
    private String fv001;// label thư mục
    private String keysearch = "Nhập từ khóa";
    private int typesearch = 0;
    List<M650> listM650;
    private List<SelectItem> listF000 = new ArrayList<SelectItem>();// COmbo thư mục
    private int Offset = 0;
    private int Limit = 30;
    private int totalrow; // tong so dong
    private int totalpage; // tong so trang
    private int current; // traung hien tai
    static private int numrow = 30; // so dong tren 1 trang
    private int start; // bat dau lay tu record
    private int end; // lay tu record so
    private int startpage = 1; // page bat dau
    private int endpage = 1;   // page ket thuc
    private String pagename = "";
    private int totalcurent;
    private int typesearchcurent;
    private String savelink;
    
    public M650Controller() {
        getKapiMenu().setAdmin(false);
        getKapiMenu().setMainmenu1("m181");
        Object pnPN100 = getRequest().getParameter("id");
        Object action = getRequest().getParameter("action");
        System.out.println("------id------------------" + pnPN100);
        Object pnPM650 = getRequest().getParameter("pm650");
        Object pvtable = getRequest().getParameter("table");
        Object pvpage = getRequest().getParameter("page");
        Object objFS200 = getRequest().getParameter("fs200");
        Object objFV050 = getRequest().getParameter("fv050");
         Object objFF00 = getRequest().getParameter("PF000");
        try {
            if (action != null && action.equals("folder")) {
                pf000 = Util.parseInt(objFF00);
               
                SessionProvider.setSessionMapValue("folder", pf000);
            }else if(action != null && action.equals("linkview") ){
                Object viewP = getRequest().getParameter("view");
                if(viewP!=null){
                    savelink = viewP.toString();
                    SessionProvider.setSessionMapValue("LINKSAVE", savelink);
                }
                 System.out.println("--------URL LINK=========" + savelink);
            }
            
        } catch (Exception e) {
        }
        
        if (objFS200 != null) {
            this.fs200 = Integer.parseInt(objFS200.toString());
        }
        if (objFV050 != null) {
            this.fv050 = Integer.parseInt(objFV050.toString());
        }
        if (pvtable != null) {
            table = pvtable.toString();
        }
        if (pvpage != null && pvpage.toString().contains("employee.jsp")) {
            table = "N100";
        }
        if (pvpage != null && pvpage.toString().contains("supplier.jsp")) {
            
            table = "K100";
            System.out.println("tungggggggggggggggggggggg------------" + table);
        }
        if (pvpage != null && pvpage.toString().contains("partner.jsp")) {
            table = "K100";
        }
        if (pvpage != null && pvpage.toString().contains("customer.jsp")) {
            table = "N100";
        }
        if (pvpage != null && pvpage.toString().contains("cuscompany.jsp")) {
            table = "K100";
        }
        System.out.println("---->+table" + table);
        
        if (pnPN100 != null && !pnPN100.toString().isEmpty()) {
            pn100 = Integer.parseInt(pnPN100.toString());
        }
        // set right upload document
        if (pvpage != null && pvpage.toString().equals("customer.jsp")) {
            if (loginSession.getRight().isCUS_DOC_CRE()) {
                add = true;
            }
            if (loginSession.getRight().isCUS_DOC_MOD()) {
                edit = true;
            }
            if (loginSession.getRight().isCUS_DOC_DEL()) {
                delete = true;
            }
            
        } else if (pvpage != null && pvpage.toString().equals("cuscompany.jsp")) {
            if (loginSession.getRight().isCOM_DOC_CRE()) {
                add = true;
            }
            if (loginSession.getRight().isCOM_DOC_MOD()) {
                edit = true;
            }
            if (loginSession.getRight().isCOM_DOC_DEL()) {
                delete = true;
            }
        } else if (pvpage != null && pvpage.toString().equals("employee.jsp")) {
            if (loginSession.getRight().isEMP_DOC_CRE()) {
                add = true;
            }
            if (loginSession.getRight().isEMP_DOC_MOD()) {
                edit = true;
            }
            if (loginSession.getRight().isEMP_DOC_DEL()) {
                delete = true;
            }
        } else if (pvpage != null && pvpage.toString().equals("supplier.jsp")) {
            if (loginSession.getRight().isSUP_DOC_CRE()) {
                add = true;
            }
            if (loginSession.getRight().isSUP_DOC_MOD()) {
                edit = true;
            }
            if (loginSession.getRight().isSUP_DOC_DEL()) {
                delete = true;
            }
        } else if (pvpage != null && pvpage.toString().equals("partner.jsp")) {
            if (loginSession.getRight().isPAR_DOC_CRE()) {
                add = true;
            }
            if (loginSession.getRight().isPAR_DOC_MOD()) {
                edit = true;
            }
            if (loginSession.getRight().isPAR_DOC_DEL()) {
                delete = true;
            }
        }


        //paging
        int a;
        System.out.println("D:-- listEmployee is not null ");
        Object ppage = getRequest().getParameter("p");
        if (ppage != null) {
            try {
                a = Integer.parseInt(ppage.toString());
            } catch (Exception ex) {
                a = 1;
            }
            if (a == 1) {
                setCurrent(1);
                setOffset(0);
                System.out.println("D:-- setOffset: " + 0);
            } else {
                setCurrent(a);
                setOffset((a - 1) * Limit);
                System.out.println("D:-- setOffset: " + ((a - 1) * Limit));
            }
            System.out.println("D:-- page: " + a);
            
        } else {
            SessionProvider.setSessionMapValue("typeSearchM650", null);
            SessionProvider.setSessionMapValue("keySearchM650", null);
            setCurrent(1);
            setOffset(0);
            System.out.println("D:-- page: 1");
        }
        
    }
    
    public void reload() {
        System.out.println("---change+++++++++++++" + getKeysearch() + typesearch);
        SessionProvider.setSessionMapValue("KEY", keysearch);
        SessionProvider.setSessionMapValue("TYPE", typesearch);
        SessionProvider.setSessionMapValue("submitSearch", "false");
        SessionProvider.setSessionMapValue("typeSearchM650", typesearch);
        SessionProvider.setSessionMapValue("keySearchM650", keysearch);
    }

    /**
     * @return the pn100
     */
    public int getPn100() {
        return pn100;
    }

    /**
     * @param pn100 the pn100 to set
     */
    public void setPn100(int pn100) {
        this.pn100 = pn100;
    }

    /**
     * @return the pm650
     */
    public List<M650> getDocumentAll() {
        try {
            M650Store attachmentStore = new M650Store();
            int type = 0;
            if (SessionProvider.getSessionMapValue("TYPE") != null) {
                type = Util.parseInt(SessionProvider.getSessionMapValue("TYPE"));
            } else {
                type = 0;
            }
            
            String key = null;
            if (SessionProvider.getSessionMapValue("KEY") != null) {
                key = SessionProvider.getSessionMapValue("KEY").toString();
            }
            Object o = SessionProvider.getSessionMapValue("typeSearchM650");
            if (o != null) {
                int t;
                try {
                    t = Integer.parseInt(o.toString());
                } catch (Exception e) {
                    t = 0;
                }
                setTypesearch(t);
                
            }
            
            Object o2 = SessionProvider.getSessionMapValue("keySearchM650");
            if (o2 != null) {
                setKeysearch(o2.toString());
                
                
                
            }
            M650 m650;
            if (type == 1) {
               
                listM650 = attachmentStore.getListDocument_NA(getLogin().getModule(), getLogin().getCompanyNo(), 0, 0, key, getLogin().getUserName());
                System.out.println("============TYPE 1 NAME==================== : " + listM650.size());
                
                
            } else if (type == 4) {
                listM650 = attachmentStore.getListDocument_PA(getLogin().getModule(), getLogin().getCompanyNo(), 0, 0, key, getLogin().getUserName());
                System.out.println("================================ : " + listM650.size());
                
                
            } else if (type == 5) {
                listM650 = attachmentStore.getListDocument_TU(getLogin().getModule(), getLogin().getCompanyNo(), 0, 0, key, getLogin().getUserName());
               
                
                
            } else if (type == 6) {
                
                listM650 = attachmentStore.getListDocument_CT(getLogin().getModule(), getLogin().getCompanyNo(), 0, 0, key, getLogin().getUserName());
                System.out.println("==================TU============== : " + listM650.size());
               
            } else if (type == 0) {
                 System.out.println("===========PARAM============+++++========= : " + getLogin().getModule()+","+ getLogin().getCompanyNo()+","+ 0+","+ null+","+ 0+","+ 0+","+ 0+","+ null+","+ table+","+ 0+","+ pn100+","+ null+","+null+","+Offset+","+ Limit+","+ 0+","+0+","+ getLogin().getUserName());
                listM650 = attachmentStore.findM650(getLogin().getModule(), getLogin().getCompanyNo(), 0, null, 0, pf000, 0, null, table, 0, pn100, null, null,Offset, Limit, 0,0, getLogin().getUserName());
                System.out.println("================================ : " + listM650.size() + table + pn100);
                if (Offset == 0) {
                    if (listM650.size() > 0) {
                        m650 = listM650.get(0);
                        if (m650 != null) {
                            SessionProvider.setSessionMapValue("totalcurentM650", m650.getRowss());
                            //totalcurent = n100.getRowss();
                        } else {
                            SessionProvider.setSessionMapValue("totalcurentM650", 0);
                            //totalcurent = 0;
                        }
                    } else {
                        SessionProvider.setSessionMapValue("totalcurentM650", 0);
                        //totalcurent = 0;
                    }
                }
                
            }

            //paging
            int t;
            Object obj = SessionProvider.getSessionMapValue("totalcurentM650");
            if (obj != null) {
                try {
                    t = Integer.parseInt(obj.toString());
                } catch (Exception e) {
                    t = 0;
                }
                totalcurent = t;
            } else {
                totalcurent = 0;
            }
            
            setTotalrow(totalcurent);
            
            SessionProvider.removeSessionMapValue("TYPE");
            return listM650;
            
        } catch (Exception ex) {
            System.out.println("ERROR DOCUMENT : " + ex.getMessage());
            return null;
        }
        
    }
    
    public int getPm650() {
        M650Store attachmentStore = new M650Store();
        avata = "../images/avatar2.jpg";
        setListdocument(new ArrayList<M650>());
        System.out.println("size of list....." + loginSession.getLogin().getModule());
        try {
            System.out.println("------------------------->>fv050:.." + fv050);
            System.out.println("------------------------->>pn100:.." + pn100);
            System.out.println("4444444444444444444444444444444444444444:.." + loginSession.getLogin().getModule() + "," + loginSession.getLogin().getCompanyNo() + "," + 0 + "," + null + "," + 0 + "," + 0 + "," + 0 + "," + null + "," + table + "," + fv050 + "," + pn100 + "," + null + "," + null + "," + loginSession.getLogin().getUserName());
            setListdocument(attachmentStore.findM650(loginSession.getLogin().getModule(), loginSession.getLogin().getCompanyNo(), 0, null, 0, 0, 0, null, table, fv050, pn100, null, null,0,10000,0,0, loginSession.getLogin().getUserName()));
            System.out.println("size of list......." + getListdocument().size());
            
        } catch (Exception e) {
            System.out.println("error database" + e.getMessage());
        }
        return pm650;
    }

    /**
     * @param pm650 the pm650 to set
     */
    public void setPm650(int pm650) {
        this.pm650 = pm650;
    }

    /**
     * @return the avata
     */
    public String getAvata() {
        
        return avata;
    }

    /**
     * @param avata the avata to set
     */
    public void setAvata(String avata) {
        this.avata = avata;
    }

    /**
     * @return the listdocument
     */
    public List<M650> getListdocument() {
        if (listdocument.size() > 0) {
            setFlag("block");
        }
        System.out.println("-------size document " + listdocument.size());
        return listdocument;
    }

    // List F000, thư mục tài liệu
    public List<F000> getListAllF000() {
        
        try {
            F000Store f000Store = new F000Store();
            List<F000> list = f000Store.getListCityALL(getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getUserName());
            return list;
        } catch (Exception ex) {
            System.out.println(ex);
            return null;
        }
        
    }

    /**
     * @param listdocument the listdocument to set
     */
    public void setListdocument(List<M650> listdocument) {
        this.listdocument = listdocument;
    }

    /**
     * @return the flag
     */
    public String getFlag() {
        return flag;
    }

    /**
     * @param flag the flag to set
     */
    public void setFlag(String flag) {
        this.flag = flag;
    }

    /**
     * @return the add
     */
    public boolean isAdd() {
        return add;
    }

    /**
     * @param add the add to set
     */
    public void setAdd(boolean add) {
        this.add = add;
    }

    /**
     * @return the edit
     */
    public boolean isEdit() {
        return edit;
    }

    /**
     * @param edit the edit to set
     */
    public void setEdit(boolean edit) {
        this.edit = edit;
    }

    /**
     * @return the delete
     */
    public boolean isDelete() {
        return delete;
    }

    /**
     * @param delete the delete to set
     */
    public void setDelete(boolean delete) {
        this.delete = delete;
    }

    /**
     * @return the fv050
     */
    public int getFs200() {
        return fs200;
    }

    /**
     * @param fv050 the fv050 to set
     */
    public void setFs200(int fs200) {
        this.fs200 = fs200;
    }

    /**
     * @return the active
     */
    public String getActive() {
        return active;
    }

    /**
     * @param active the active to set
     */
    public void setActive(String active) {
        this.active = active;
    }

    /**
     * @return the fv050
     */
    public int getFv050() {
        return fv050;
    }

    /**
     * @param fv050 the fv050 to set
     */
    public void setFv050(int fv050) {
        this.fv050 = fv050;
    }

    /**
     * @return the keysearch
     */
    public String getKeysearch() {
        return keysearch;
    }

    /**
     * @param keysearch the keysearch to set
     */
    public void setKeysearch(String keysearch) {
        this.keysearch = keysearch;
    }

    /**
     * @return the typesearch
     */
    public int getTypesearch() {
        return typesearch;
    }

    /**
     * @param typesearch the typesearch to set
     */
    public void setTypesearch(int typesearch) {
        this.typesearch = typesearch;
    }

    /**
     * @return the listF000
     */
    public List<SelectItem> getListF000() {
        ComboboxUtil combo = new ComboboxUtil();
        listF000 = combo.getlistCombF000();
        return listF000;
    }

    /**
     * @param listF000 the listF000 to set
     */
    public void setListF000(List<SelectItem> listF000) {
        this.listF000 = listF000;
    }

    /**
     * @return the pf000
     */
    public int getPf000() {
        if (SessionProvider.getSessionMapValue("folder") != null) {
            pf000 = Util.parseInt(SessionProvider.getSessionMapValue("folder"));
        }
        return pf000;
    }

    /**
     * @param pf000 the pf000 to set
     */
    public void setPf000(int pf000) {
        this.pf000 = pf000;
    }

    /**
     * @return the fv001
     */
    public String getFv001() {
        return fv001;
    }

    /**
     * @param fv001 the fv001 to set
     */
    public void setFv001(String fv001) {
        this.fv001 = fv001;
    }

    /**
     * @return the Offset
     */
    public int getOffset() {
        return Offset;
    }

    /**
     * @param Offset the Offset to set
     */
    public void setOffset(int Offset) {
        this.Offset = Offset;
    }

    /**
     * @return the Limit
     */
    public int getLimit() {
        return Limit;
    }

    /**
     * @param Limit the Limit to set
     */
    public void setLimit(int Limit) {
        this.Limit = Limit;
    }

    // property for paging
    /**
     * @return the current
     */
    public int getCurrent() {
        if (current > totalpage) {
            current = totalpage;
        } else if (current < 1) {
            current = 1;
        }
        return current;
    }

    /**
     * @param current the current to set
     */
    public void setCurrent(int current) {
        
        start = (current - 1) * getNumrow();
        end = current * numrow;
        this.current = current;
    }

    /**
     * @return the numrow
     */
    public int getNumrow() {
        if (numrow == 0) {
            numrow = 30;
        }
        return numrow;
    }

    /**
     * @param numrow the numrow to set
     */
    public void setNumrow(int numrow) {
        this.numrow = numrow;
    }

    /**
     * @return the start
     */
    public int getStart() {
        if (start == 0) {
            start = 1;
        }
        return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(int start) {
        this.start = start;
    }

    /**
     * @return the end
     */
    public int getEnd() {
        if (end == 0) {
            end = 20;
        }
        return end;
    }

    /**
     * @param end the end to set
     */
    public void setEnd(int end) {
        this.end = end;
    }

    /**
     * @return the totalrow
     */
    public int getTotalrow() {
        return totalrow;
    }

    /**
     * @param totalrow the totalrow to set
     */
    public void setTotalrow(int totalrow) {
//        if (totalrow == 0) {
//            totalrow = 100;
//        }
        totalpage = totalrow / numrow;
        
        if (totalrow % numrow > 0) {
            totalpage = totalpage + 1;
        }
        
        
        if (getTotalpage() <= 10) {
            setStartpage(1);
            setEndpage(getTotalpage());
            
        } else {
            setStartpage(current - 5);
            setEndpage(current + 5);
            
            
        }
        this.totalrow = totalrow;
    }

    /**
     * @return the startpage
     */
    public int getStartpage() {
        if (startpage < 1) {
            startpage = 1;
        }
        return startpage;
    }

    /**
     * @param startpage the startpage to set
     */
    public void setStartpage(int startpage) {
        this.startpage = startpage;
    }

    /**
     * @return the endpage
     */
    public int getEndpage() {
        if (endpage > totalpage) {
            endpage = totalpage;
        }
        return endpage;
    }

    /**
     * @param endpage the endpage to set
     */
    public void setEndpage(int endpage) {
        
        this.endpage = endpage;
    }

    /**
     * @return the totalpage
     */
    public int getTotalpage() {
//        if (totalpage == 0) {
//            totalpage = 10;
//        }
        return totalpage;
    }

    /**
     * @param totalpage the totalpage to set
     */
    public void setTotalpage(int totalpage) {
        this.totalpage = totalpage;
    }

    /**
     * @return the pagename
     */
    public String getPagename() {
        return pagename;
    }

    /**
     * @param pagename the pagename to set
     */
    public void setPagename(String pagename) {
        this.pagename = pagename;
    }

    /**
     * @return the totalcurent
     */
    public int getTotalcurent() {
        return totalcurent;
    }

    /**
     * @param totalcurent the totalcurent to set
     */
    public void setTotalcurent(int totalcurent) {
        this.totalcurent = totalcurent;
    }

    /**
     * @return the typesearchcurent
     */
    public int getTypesearchcurent() {
        return typesearchcurent;
    }

    /**
     * @param typesearchcurent the typesearchcurent to set
     */
    public void setTypesearchcurent(int typesearchcurent) {
        this.typesearchcurent = typesearchcurent;
    }

    /**
     * @return the savelink
     */
    public String getSavelink() {
        if( SessionProvider.getSessionMapValue("LINKSAVE")!=null){
            savelink = SessionProvider.getSessionMapValue("LINKSAVE").toString();
        }
        return savelink;
    }

    /**
     * @param savelink the savelink to set
     */
    public void setSavelink(String savelink) {
        this.savelink = savelink;
    }
}
